package com.inossem.warehousestorage_inossem.request.outStorage;

import android.content.Context;

import com.google.gson.Gson;
import com.inossem.utils.log.LogUtils;
import com.inossem.warehousestorage_inossem.bean.outStorage.BatchBean;
import com.inossem.warehousestorage_inossem.constant.RequestConstant;
import com.inossem.warehousestorage_inossem.constant.TypeEnum;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.listener.StringRequestListener;
import com.inossem.warehousestorage_inossem.manage.RequestManage;
import com.inossem.warehousestorage_inossem.model.StringResponse;
import com.inossem.warehousestorage_inossem.request.Request;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by leij on 2017/6/12.
 */

public class GetDefaultBatchRequest extends Request {

    private static final String MATERIEL_CODE = "mat_id";//物料编码

    private static final String NUMBER = "output_qty";//数量

    private static final String INVENTORYA_DDRESS_CODE = "location_id";//库存地点

    private static final String BILL_TYPE = "receipt_type";//单据类型

    private static final String SPECIAL_INVENTORY_CODE = "spec_stock";//特殊库存代码

    public static String request(Context context, String materielCode, String number, String inventoryAddress, String specialInventoryCode, TypeEnum type, GsonBeanListener gsonBeanListener) {
        String tag;
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put(MATERIEL_CODE, materielCode);
//            jsonObject.put(WERKS, werks);
            jsonObject.put(INVENTORYA_DDRESS_CODE, inventoryAddress);
            jsonObject.put(NUMBER, number);
            switch (type) {
                case OUT_STORAGE_SALE:
                    jsonObject.put(BILL_TYPE, "42");
                    break;
                case OUT_STORAGE_OTHER:
                    jsonObject.put(BILL_TYPE, "44");
                    jsonObject.put(SPECIAL_INVENTORY_CODE, specialInventoryCode);
                    break;
            }
        } catch (JSONException e) {
            LogUtils.logCatchExceptionE(context, "", e);
            e.printStackTrace();
        }
        String methodUrl = "";
        switch (type) {
            case OUT_STORAGE_PICKING:
                methodUrl = RequestConstant.URL_OUT_STORAGE_PICKING_DEFAULT_BATCH;
                break;
            case OUT_STORAGE_SALE:
                methodUrl = RequestConstant.URL_OUT_STORAGE_SALE_DEFAULT_BATCH;
                break;
            case OUT_STORAGE_RETURN:
                methodUrl = RequestConstant.URL_OUT_STORAGE_RETURN_DEFAULT_BATCH;
                break;
            case OUT_STORAGE_OTHER:
                methodUrl = RequestConstant.URL_OUT_STORAGE_OTHER_DEFAULT_BATCH;
                break;
            case OUT_STORAGE_ALLOCATING:
                methodUrl = RequestConstant.URL_OUT_STORAGE_ALLOCATING_DEFAULT_BATCH;
                break;
            case OUT_STORAGE_PRODUCTION:
                methodUrl = RequestConstant.URL_OUT_STORAGE_PRODUCTION_DEFAULT_BATCH;
                break;
        }

        String json = jsonObject.toString();
        String url = RequestManage.getUrl(context, methodUrl);
        tag = RequestManage.getHttpsPostWithLoading(context, url, json, new StringRequestListener() {
            @Override
            public void onSuccess(StringResponse stringRequestResponse) {
                Gson gson = new Gson();
                BatchBean bean = gson.fromJson(stringRequestResponse.getResponseInfo(), BatchBean.class);
                excuteJsonBeanNew(context, bean, gsonBeanListener);
            }
        });
        return tag;
    }

}
